<?php
    header('content-type: text/html; charset=utf-8');
    include_once 'DBLink.php';
    $method=$_REQUEST['method'];
    $dblink=new DBLink();
    switch ($method) {
        case 'a-login':
            $uname=$_REQUEST['uname'];
            $upwd=$_REQUEST['upwd'];
            if($uname=='' || $upwd==''){
                echo "<script>alert('账号或密码为空，请重新输入') ;</script>";
                header("refresh:1;url=../index.html");
	    }
	    $conn=$dblink->getConnect();
	    $res = "SELECT uname FROM admin WHERE uname='$uname'";
	    if(!mysqli_fetch_row($dblink->query($res,$conn)))
	    {
		    echo "<script>alert('账户名未注册，请注册！') ;</script>";
		    header("refresh:1;url=../index.html");
	    }
	    else{
		    $sql="SELECT uname,upwd FROM admin WHERE uname='$uname' AND upwd='$upwd'";
		    if(mysqli_fetch_row($dblink->query($sql,$conn))){
			    session_start();
			    $_SESSION['username']=$uname;
			    $_SESSION['password']=$upwd;
			    echo "<script>alert('登录成功！') ;</script>";
			    header("refresh:0;url=adminIndex.php");
		    }
		    else{
			    echo "<script>alert('账号或密码错误，请重新输入') ;</script>";
			    header("refresh:1;url=../index.html");
		    }
		    $dblink->close($conn);
	    }
            break;
        case 'change':
            $uname=$_REQUEST['uname'];
            $upwd=$_REQUEST['npwd'];
            $sql="UPDATE admin SET upwd='$upwd' WHERE uname='$uname'";
            $conn=$dblink->getConnect();
            $rel=$dblink->query($sql,$conn);
            if($rel!=null){
                echo "<script>alert('密码修改成功，请使用新密码重新登录')</script>";
                header("refresh:1;url=../index.html");
            }
            else{
                echo "<script>alert('修改失败，请重试')history.go(-1);</script>";
            }
            $dblink->close($conn);
            break;
        case 'search':
            $time=$_REQUEST['time'];
            if($time==''){
                echo "<script>alert('搜索的时间为空，请重新输入') ;history.go(-1);</script>";
            }
            else{
                $conn=$dblink->getConnect();
                $sql="SELECT * FROM fire WHERE time='$time'";
                $result=$dblink->query($sql,$conn);
                $row=mysqli_fetch_assoc($result);
                if($row!=null){
                    session_start();
                    $_SESSION['time']=$row['time'];
                    $_SESSION['temperature']=$row['temperature'];
                    $_SESSION['smoke']=$row['smoke'];
                    header("refresh:0;url=data_search.php");
                }
                else{
                    echo "<script>alert('该时间不存在') ;history.go(-1);</script>";
                }
                $dblink->close($conn);
            }
            break;
        case 'delete':
            $time=$_REQUEST['time'];
            $sql="DELETE FROM fire WHERE time='$time'";
            $conn=$dblink->getConnect();
            if($dblink->query($sql,$conn)){
                header("refresh:0;url=data_list.php");
            }
            else{
                echo "<script>alert('删除失败，请重试')</script>";
                header("refresh:1;url=data_list.php");
            }
            break;
        case 'logout':
            session_start();
            session_destroy();
            header("refresh:0;url=../index.html");
            break;
        default:
            echo "<script>alert('数据出错') ;</script>";
            break;
    }

?>
